﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PerfAnalyzerLib
{
	public class CounterInfo
	{
		public string Machine;
		public string Category;
		public string Instance;
		public string Counter;
		public string Fullname;

		public CounterInfo(string fullname)
		{
			if (string.IsNullOrWhiteSpace(fullname))
			{
				throw new ArgumentException("Argument null or empty.", "fullname");
			}

			this.Fullname = fullname;
			fullname = fullname.Trim(new char[] {' ', '\\'});

			string[] splits = fullname.Split(new char[] {'\\'}, StringSplitOptions.RemoveEmptyEntries);

			if (splits.Length != 3)
			{
				throw new ArgumentException("Invalid format for full counter name: " + fullname);
			}

			Machine = splits[0];

			string categoryInstance = splits[1];

			int indexStart = categoryInstance.IndexOf('(');
			int indexEnd = categoryInstance.IndexOf(')');

			if ((indexStart > 0 && indexEnd > 0) && indexStart < indexEnd)
			{
				Category = categoryInstance.Substring(0, indexStart);
				Instance = categoryInstance.Substring(indexStart + 1, indexEnd - indexStart - 1);
			}
			else
			{
				Category = categoryInstance;
			}

			Counter = splits[2]; 
		}

		/// <summary>
		/// Get the extanded name of the counter.
		/// Format: Category(Instance)\Counter or Category\Counter.
		/// </summary>
		/// <returns>Extended counter name.</returns>
		public string GetExtendedCounterName()
		{
			string name = Category;

			if (!string.IsNullOrEmpty(Instance))
			{
				name += "(" + Instance + ")";
			}

			name += "\\" + Counter;

			return name;
		}
	}
}
